unit Rails.Vcl.Factory;

interface

uses
  Rails.Metadata, Rails.ActiveRecord.Attributes, Aurelius.Mapping.Explorer,
  Vcl.ComCtrls, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Controls, Rails.Vcl.Interfaces, System.Variants,
  Rails.Vcl.Components;
type

  TConstructorFields = class(TInterfacedObject, IConstructorFields)
  private
    FColumn: TRailsColumn;
    FPanelFields: TPanel;
    FLabelTitle: TCustomLabel;
    procedure setLabelTitle(const Value: TCustomLabel);
  public
    constructor Create(const Column: TRailsColumn; const PanelFields: TPanel);
    procedure Edit(var Edit: TRailsEdit);
    procedure Title(var ALabel: TLabel);
    procedure RichEdit(var RichEdit: TRichEdit);
    procedure Memo(var Mmo: TMemo);
    procedure ComboBox(var Cmb: TRailsComboBox);
    procedure DateTimePicker(Var Dtt: TDateTimePicker);
    procedure RailsDate(Var Rdt: TRailsDate);
    procedure RailsTime(Var Rtm: TRailsTime);
    procedure RailsDateTime(Var Rdt: TRailsDateTime);
    procedure CheckBox(Var chk: TCheckBox);
    property LabelTitle: TCustomLabel read FLabelTitle write setLabelTitle;
  end;

implementation

uses
  TypInfo, RTTI, System.Classes, DB,
  Rails.Utils, Rails.View.Settings, Aurelius.Mapping.Metadata, System.Math;

{ TConstructorFields }

procedure TConstructorFields.CheckBox(var chk: TCheckBox);
begin
  chk := TCheckBox.Create(FPanelFields);
  chk.Parent := FPanelFields;
  chk.Name := 'chk'+FColumn.ColumnName;
  chk.Width := chk.Height+FLabelTitle.Width;
  chk.Left := 4;
  chk.Top := 4;
  chk.Caption := TRailsUtils.StringRepeat('  ',Length(FLabelTitle.Caption));
  FLabelTitle.Align := alNone;
  FLabelTitle.Left := chk.Height+4;
  FLabelTitle.Top := 5;
  FPanelFields.Width := chk.Width+8;
  FPanelFields.Height := chk.Height+8;
end;

procedure TConstructorFields.ComboBox(var Cmb: TRailsComboBox);
var
  EnumTyp: TRttiEnumerationType;
  vName: string;
  I, MaxLenght: Integer;
begin
  MaxLenght := 0;
  Cmb := TRailsComboBox.Create(FPanelFields);
  Cmb.AutoOpen := True;
  Cmb.Parent := FPanelFields;
  Cmb.Align := alClient;
  Cmb.Name := 'cmb'+FColumn.ColumnName;
  Cmb.Style := TComboBoxStyle.csDropDownList;
  Cmb.Items.Clear;


  if FColumn.ColumnType = ctEnumerator then
  begin
    EnumTyp := (FColumn.ColumnInfo.Optimization.RealType as TRttiEnumerationType);
    for I := EnumTyp.MinValue to EnumTyp.MaxValue do
    begin
      vName := GetEnumName(EnumTyp.Handle, I);
      Cmb.Items.Add(vName);
      if Length(vName) > MaxLenght  then
        MaxLenght := Length(vName);
    end;
    FPanelFields.Width := MaxLenght*8;
  end;
end;

constructor TConstructorFields.Create(const Column: TRailsColumn; const PanelFields: TPanel);
begin
  FColumn := Column;
  FPanelFields := PanelFields;
end;

procedure TConstructorFields.DateTimePicker(var Dtt: TDateTimePicker);
begin
  Dtt := TDateTimePicker.Create(FPanelFields);
  Dtt.Name := 'dtt'+FColumn.ColumnName;
  Dtt.Parent := FPanelFields;
  Dtt.Width := 100;
  FPanelFields.Width := Dtt.Width;
end;

procedure TConstructorFields.Edit(var Edit: TRailsEdit);
begin
  if FColumn.ColumnType = ctString then
    FPanelFields.Width :=  FColumn.Length*6;
  Edit := TRailsEdit.Create(FPanelFields);
  Edit.Parent := FPanelFields;
  Edit.Name := 'edt'+FColumn.ColumnName;
  Edit.Text := '';
  Edit.LabelTitle := LabelTitle;

  Edit.Validations := FColumn.Validations;
  case FColumn.ColumnType of
    ctString: Edit.MaxLength := FColumn.Length;
    ctInteger: Edit.MaxLength := IfThen(FColumn.ColumnInfo.FieldType = ftLargeint, 20, 11);
    ctFloat:
    begin
      Edit.Precision := FColumn.Precision;
      Edit.Decimals := FColumn.Scale;
      Edit.isDecimal := True;
      if (LocalSettings.InputBank = ibAll) or (rvMoney in Edit.Validations) then
        FPanelFields.Width := Trunc(FPanelFields.Width*1.4)+15
      else
        FPanelFields.Width := Trunc(FPanelFields.Width*1.4);
    end;
  end;
end;

procedure TConstructorFields.Memo(var Mmo: TMemo);
begin

end;

procedure TConstructorFields.RailsDate(var Rdt: TRailsDate);
begin
  Rdt := TRailsDate.Create(FPanelFields);
  Rdt.Name := 'rdt'+FColumn.ColumnName;
  FPanelFields.Width := 136;
end;

procedure TConstructorFields.RailsDateTime(var Rdt: TRailsDateTime);
begin
  Rdt := TRailsDateTime.Create(FPanelFields);
  Rdt.Parent := FPanelFields;
  Rdt.Name := 'rdt'+FColumn.ColumnName;
  FPanelFields.Width := 214;
end;

procedure TConstructorFields.RailsTime(var Rtm: TRailsTime);
begin

end;

procedure TConstructorFields.RichEdit(var RichEdit: TRichEdit);
begin

end;

procedure TConstructorFields.setLabelTitle(const Value: TCustomLabel);
begin
  FLabelTitle := Value;
end;

procedure TConstructorFields.Title(var ALabel: TLabel);
begin
  ALabel := TLabel.Create(FPanelFields);
  ALabel.Parent := FPanelFields;
  ALabel.Name := 'lbl'+FColumn.ColumnName;
  ALabel.Caption := FColumn.DisplayName;
end;

end.
